import { defineStore } from 'pinia'
import { ref } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

export const useUserStore = defineStore('user', () => {
  const currentUser = ref(sessionStorage.getItem('username'))

  const login = async (credentials) => {
    const resp = await request.post('/zhsj/api/login', null, {
      params: {
        syscode: credentials.username,
        syspwd: credentials.password
      }
    })
    if (resp.code != 20000) {
      ElMessage.error(resp.msg)
      return false
    }
    sessionStorage.setItem('username', credentials.username)
    ElMessage.success('登录成功')
    currentUser.value = credentials.username
    return true
  }

  const logout = () => {
    sessionStorage.removeItem('username')
    currentUser.value = null
  }

  return {
    currentUser,
    login,
    logout
  }
}) 